<?php
namespace App\Http\Controllers;

use App\Models\TopicM;
use App\Common\excuteState;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Database\QueryException;
use App\Models\TopicFavourM;
use App\Models\TopicFollowM;

class TopicController extends Controller
{
 
    /**
     * 检索 话题基本信息
     */
    static  public function loadTpcBase(){
        DB::beginTransaction();
        try {
            $tpcs=TopicM::loadTpcBase();
            DB::commit();
            return excuteState::OKwithDatas($tpcs);
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::selectError();
        }
    }
    
    
    /**
     * load all topics
     */
    static public function loadTpc($userid){
//         DB::beginTransaction();
//         try {
            $tpcs=TopicM::loadAllTpc($userid);
            DB::commit();
            return excuteState::OKwithDatas($tpcs);
//         } catch (QueryException $e) {
//             DB::rollBack();
//             return excuteState::selectError();
//         }
    }
    
    /**
     * add topic  ['title'=>'title 1','content'=>"content 1"]
     * @param Request $request
     */
   static public function addTpc(Request $request){
       DB::beginTransaction();
       try {
            $title=$request->input('title');
            $dscrpt=$request->input('content');
            TopicM::addTpc(['title'=>$title,"content"=>$dscrpt]);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::insertError();
        }
    }
    
    /**
     * 话题浏览数自增
     * @param unknown $tpcID
     */
    static  public function plusTcpBrowse($tpcID){
        DB::beginTransaction();
        try {
            TopicM::plusTcpBrowse($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    /**
     * 话题浏览数 自减
     * @param unknown $tpcID
     */
    static  public function minTcpBrowse($tpcID){
        DB::beginTransaction();
        try {
            TopicM::minTcpBrowse($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    /**
     * icon num 自增
     * @param unknown $tpcID
     */
    static  public function plusTcpIcnNum($tpcID){
        DB::beginTransaction();
        try {
            TopicM::plusTcpIcnNum($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    /**
     * icon num 自减
     * @param unknown $tpcID
     */
    static  public function minTcpIcnNum($tpcID){
        DB::beginTransaction();
        try {
            TopicM::minTcpIcnNum($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    
    /**
     * stored num 自增
     * @param unknown $tpcID
     */
    static  public function plusTcpFavour($userid,$tpcID){
        DB::beginTransaction();
        try {
            // 写入 topic-favour 记录
            TopicFavourM::addItem(['userid'=>$userid,'topicid'=>$tpcID]);
            // topic favour num ++
            TopicM::plusTcpFavour($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    /**
     * stored num 自减
     * @param unknown $tpcID
     */
    static  public function minTcpFavour($userid,$tpcID){
        DB::beginTransaction();
        try {
            
            // 写入 topic-favour 记录
            TopicFavourM::dltItem(['userid'=>$userid,'topicid'=>$tpcID]);
            // topic favour num --        
            TopicM::minTcpFavour($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    
    
    /**
     * follow num 自增
     * @param unknown $tpcID
     */
    static  public function plusTcpFollow($userid,$tpcID){
        DB::beginTransaction();
        try {
            // 写入 topic-follow 记录
            TopicFollowM::addItem(['userid'=>$userid,'topicid'=>$tpcID]);
            // topic follow num ++
            TopicM::plusTcpFollow($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
    /**
     * follow num 自减
     * @param unknown $tpcID
     */
    static  public function minTcpFollow($userid,$tpcID){
        DB::beginTransaction();
        try {
    
            // 写入 topic-follow 记录
            TopicFollowM::dltItem(['userid'=>$userid,'topicid'=>$tpcID]);
            // topic follow num --
            TopicM::minTcpFollow($tpcID);
            DB::commit();
            return excuteState::OK();
        } catch (QueryException $e) {
            DB::rollBack();
            return excuteState::updateError();
        }
    }
    
    
}

?>